Almost all programs use functions to break-up large programs into manageable and reusable chunks of code. Related functions are usually grouped into separate files which are independently compiled. When using functions from a separate file or library, you must use the "extern" keyword followed by the function prototype to tell the compiler that the particular function is not part of the file. The following is an example of how to call functions from a separate file:
// file1.cp
#include <iostream.h>
// function prototypes
extern float myInput( char *prompt );
extern float myMultiply( float a, float b );
extern void myPrintResult( float result );
void main( void )
{
float a,b,result;
a = myInput( "Input A: " );
b = myInput( "Input B: " );
result = myMultiply( a, b );
myPrintResult( result );
}
// end file1.cp
// file2.cp
#include <iostream.h>
// function prototypes
float myInput( char *prompt );
float myMultiply( float a, float b );
void myPrintResult( float result );
float myInput( char *prompt )
{
float input;
cout << prompt;
cin >> input;
return input;
}
float myMultiply( float a, float b )
{
return a * b;
}
void myPrintResult( float result )
{
cout << "The result is ";
cout << result << endl;
}
// end file2.cp
FUNCTION OVERLOADING
In C++, you can create more than one function with the same name. However, the parameter list of these "overloaded" functions must differ. When asked to call a function, the compiler will compare the parameters of the called function to the list of functions which share the same name. When a match is determined, the proper function is called.
// overload.cp
#include <iostream.h>
void DoAdd( int a, int b );
void DoAdd( float a, float b );
void main( void )
{
int i1 = 5;
int i2 = 10;
float f1 = 2.5;
float f2 = 4.1;
DoAdd( i1, i2 );
DoAdd( f1, f2 );
}
void DoAdd( int a, int b )
{
int result;
result = a + b;
cout << a << " + " << b << " = " << result << endl;
}
void DoAdd( float a, float b )
{
float result;
result = a + b;
cout << a << " + " << b << " = " << result << endl;
}
// end overload.cp
COMMAND LINE ARGUMENTS
Although the Macintosh doesn't utilize a command line prompt (at least not yet), you may need to write programs which use command line arguments.
// command.cp
#include <iostream.h>
#include <console.h> // THINK C console implementation
int main( int argc, char *argv[] )
{
int i;
argc = ccommand( &argv ); // THINK C specific
for( i = 1; i < argc; i++ )
cout << argv[i] << " ";
cout << endl;
return true;
}
// end command.cp
CALLING C FUNCTIONS FROM C++
C++ performs "name mangling" in order to perform such feats as function overloading. Basically, this means that C++ renames your functions behind the scenes. Therefore, if you need to call a C function (this usually means calling a function from an object code library which has been compiled by a straight C compiler), you need to let the C++ compiler know not to perform any name mangling. This is performed by using the following statement:
extern "C" function_prototype;
If there is a list of functions, you can use:
extern "C"
{
function_prototype1;
function_prototype2;
function_prototype3;
//...
}
To enclose a complete library, you can use:
extern "C"
{
#include <header_file.h>
}
DEFAULT ARGUMENT LIST
You can supply default values for function arguments in C++ as follows:
// default.cp
#include <iostream.h>
void myPrint( int val = 0, int times = 1 );
void main( void )
{
myPrint();
myPrint(5);
myPrint(7,3);
}
void myPrint( int val, int times )
{
for( int i=1; i<=times; i++ )
{
if( i==1 )
cout << "Output Value: " << val << endl;
else
cout << " Copy " << i << ": " << val << endl;
}
}
// end default.cp
VARIABLE-LENGTH ARGUMENT LIST
Although not recommended, you may need to create a function which has a variable argument list (such as printf and scanf). Here's how to define and use variable-length argument lists in functions:
// list.cp
#include <iostream.h>
#include <stdarg.h>
void myPrintList( char *format, ... ); // ellipsis must be last item
void main( void )
{
int int1 = 2;
int int2 = 7;
double real1 = 3.14;
myPrintList( "List:%d%d%f", int1, int2, real1 );
}
void myPrintList( char *format, ... )
{
va_list argPtr;
char *p;
int intItem;
double realItem;
va_start( argPtr, format );
for( p = format; *p; p++ ) // search for end of string